Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experiment] Ktor Call.Factory #7326

Closed
wants to merge 4 commits into from
Closed

Conversation

yschimke
Copy link
Collaborator

@yschimke yschimke commented Jun 12, 2022

Flush out bugs and implementation questions for KMP Call.Factory.
If I remember right, the Plan of Records, it we won't supply this, but it's valid to confirm it's possible.

Found

  • Bug in resolvedContentType with wrong content type string built

Limitations

  • Currently reads all request and response bodies before continuing.

Open Questions

  • How to handle, large, streaming or delayed request and response body.
  • Are there other platforms (native?) we should use this to test on.

@yschimke yschimke requested a review from swankjesse June 12, 2022 17:31
import okhttp3.RequestBody
import okhttp3.ResponseBody

actual suspend fun HttpResponse.toHttpResponseBody(): ResponseBody {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I need the most help here. buffer.write(bytes) below is blocking, so at least that should be sorted out.


actual suspend fun RequestBody.toHttpRequestBody(): Any {
val buffer = okio.Buffer()
this.writeTo(buffer)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also blocking.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Ktor doc examples aren't great here, using readBytes()

https://ktor.io/docs/request.html#upload_file

And "This function accepts different types of payloads, including plain text, arbitrary class instances, form data, byte arrays, and so on"

@swankjesse
Copy link
Collaborator

Yeah I think ktor is the wrong abstraction. It's a multiplatform API that wraps various engines. We're better off wrapping other engines directly for maximum power and performance.

@swankjesse
Copy link
Collaborator

My best idea for JS is to never stream the request or response body.

@yschimke
Copy link
Collaborator Author

My best idea for JS is to never stream the request or response body.

Sure but even this ends up with an blocking operation putting bytes into a Buffer. Is there an immediate way to get a BufferedSource from a byte array?

image

@yschimke
Copy link
Collaborator Author

Yeah I think ktor is the wrong abstraction. It's a multiplatform API that wraps various engines. We're better off wrapping other engines directly for maximum power and performance.

I like this approach, but I think it's useful here just to validate it possible and provide a template. I can close this PR off once it's the best it can be, but still have something to point people to.

I also think it's best if we provide this option, prove it works, but then let others who need this build and maintain the implementation.

@swankjesse
Copy link
Collaborator

Agreed!

@yschimke
Copy link
Collaborator Author

What would the best API be for JS? Is that a lot of work that we should take advantage of Ktor here?

Would this fly as a JS reference implementation?

# Conflicts:
#	kotlin-js-store/yarn.lock
#	okhttp/src/nonJvmMain/kotlin/okhttp3/RequestBody.kt
@yschimke yschimke reopened this Dec 28, 2022
@yschimke yschimke closed this Apr 1, 2023
@yschimke yschimke deleted the js_test branch May 27, 2023 11:25
@DRSchlaubi
Copy link

What would the best API be for JS? Is that a lot of work that we should take advantage of Ktor here?

Would this fly as a JS reference implementation?

Probabbly fetch since it is supported in browser and nodejs, it's also what ktor does: https://github.com/ktorio/ktor/tree/main/ktor-client/ktor-client-core/js/src/io/ktor/client/fetch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants